/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Desarrollo.Soft.Hibernate.ViewPrincipal.views;

import Desarrollo.Soft.Hibernate.Control.ctrlGeneric;
import Desarrollo.Soft.Hibernate.Data.Entity.Catproductos;
import Desarrollo.Soft.Hibernate.Data.Entity.Tblalmacen;
import Desarrollo.Soft.Hibernate.Data.Entity.Tblcomprasproductos;
import Desarrollo.Soft.Hibernate.Data.Entity.Tblproductosproveedores;
import Desarrollo.Soft.Hibernate.ViewPrincipal.frmPantallaPrincipal;
import javax.swing.table.DefaultTableModel;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Suriel
 */
public class intFrmCompraProducto extends javax.swing.JInternalFrame {

    /**
     * Creates new form intFrmCompraProducto
     */
    private intFrmCompra frmCompras;
    private frmPantallaPrincipal pantallaPrincipal;
    DefaultTableModel tablaCompraProducto = new DefaultTableModel();

    public intFrmCompraProducto() {
        initComponents();
    }

    public intFrmCompraProducto(intFrmCompra _compras, frmPantallaPrincipal _frmPrincipal) {
        initComponents();
        this.frmCompras = _compras;
        this.pantallaPrincipal = _frmPrincipal;

        String titulosProductos[] = {"NÚM DE PRODUCTO", "NOMBRE", "CODIGO", "STOCK", "TOTAL"};
        tablaCompraProducto.setColumnIdentifiers(titulosProductos);
        this.tblProductos.setModel(tablaCompraProducto);
        this.llenarTablaProductos();
    }

    public void seleccionarProductoAgragado(Catproductos _producto) {
        try {
//            tablaCompraProducto.;
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }

    private boolean validaciondatos() {
        try {
            if (txtCantidad.getText().equals("")) {
                return false;
            }
            if (txtUnidad.getText().equals("")) {
                return false;
            }
            if (txtPrecio.getText().equals("")) {
                return false;
            }
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblProductos = new javax.swing.JTable();
        jLabel15 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtUnidad = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPrecio = new javax.swing.JTextField();

        tblProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblProductos);

        jLabel15.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        jLabel15.setText("Productos");

        jLabel1.setText("Cantidad:");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Desarrollo/Soft/Hibernate/ViewPrincipal/Imagenes/Accept.gif"))); // NOI18N
        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Desarrollo/Soft/Hibernate/ViewPrincipal/Imagenes/Delete.png"))); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setText("Uni. de Medida");

        jLabel3.setText("Precio de Compra");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(467, 467, 467)
                        .addComponent(jLabel15))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(26, 26, 26)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(33, 33, 33)
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(70, 70, 70)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(69, 69, 69)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 936, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel15)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            if (this.validaciondatos()) {
                int i = tblProductos.getSelectedRow();
                Tblcomprasproductos comprasProductos = new Tblcomprasproductos();
                Catproductos products = new Catproductos();
                Tblalmacen almacen = new Tblalmacen();
                comprasProductos.setCantidadProducto(Integer.parseInt(txtCantidad.getText()));
                comprasProductos.setUnidadmedida(txtUnidad.getText());
                comprasProductos.setPreciounitario(Double.parseDouble(txtPrecio.getText()));
                products.setId(Integer.parseInt(tblProductos.getValueAt(i, 0).toString()));
                products.setValor((String) tblProductos.getValueAt(i, 1));
                almacen.setCantidad(Integer.parseInt(tblProductos.getValueAt(i, 2).toString()));

                this.frmCompras.agregarProducto(comprasProductos);
                this.frmCompras.cargarProductos();
                this.dispose();

            } else {
                JOptionPane.showMessageDialog(null, "Todos los datos son obligatorios.");
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblProductos;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtPrecio;
    private javax.swing.JTextField txtUnidad;
    // End of variables declaration//GEN-END:variables

    private void llenarTablaProductos() {
        try {
            double total;
            List<Catproductos> listaProductos = ctrlGeneric.<Catproductos>selectItems(Catproductos.class);
            List<Tblalmacen> almacen = ctrlGeneric.<Tblalmacen>selectItems(Catproductos.class);
            for (int i = 0; i < listaProductos.size(); i++) {
                String filas[] = new String[5];
                total = Double.parseDouble(txtCantidad + "" + txtPrecio);

                filas[0] = String.valueOf(listaProductos.get(i).getId());
                filas[1] = String.valueOf(listaProductos.get(i).getDescripcion());
                filas[2] = String.valueOf(listaProductos.get(i).getValor());
                filas[3] = String.valueOf(almacen.get(i).getCantidad());
                filas[4] = String.valueOf(total);
                tablaCompraProducto.addRow(filas);
            }
        } catch (Exception e) {
        }
    }
}
